package com.hcs.service.impl;

import com.hcs.comm.R;
import com.hcs.entity.Category;
import com.hcs.mapper.CategoryMapper;
import com.hcs.service.ICategoryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hcs
 * @since 2025-01-09 04:10
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {


    @Override
    public List<Category> list(String name) {
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        if (name != null && !name.isEmpty()) {
            queryWrapper.like("name", name);
        }
        return this.list(queryWrapper);
    }

    @Override
    public void saveWithDish(Category category) {
        this.save(category);
    }

    @Override
    public void updateWithDish(Category category) {
        this.updateById(category);
    }

    @Override
    public void removeWithDish(Long id) {
        this.removeById(id);
    }

    @Override
    public R<Page<Category>> page(int page, int pageSize, String name) {
        Page<Category> categoryPage = new Page<>(page, pageSize);
        categoryPage=  this.page(categoryPage, new QueryWrapper<Category>().like("name", name)  );

        if (categoryPage.getRecords() == null || categoryPage.getRecords().isEmpty()){
            return R.success(null);
        }
        return R.success(categoryPage);
    }


}
